﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace thinger.AutomaticStoreMotionControlLib
{
    [DefaultEvent("ClickEvent")]
    public partial class NaviButton : UserControl
    {
        public NaviButton()
        {
            InitializeComponent();
            _saveBackColor = this.BackColor;
        }

        private Color _saveBackColor;

        private Image _naviImage = Properties.Resources.icon_home_page;
        [Browsable(true)]
        [Category("自定义属性")]
        [Description("导航按钮图片设置")]
        public Image NaviImage
        {
            get { return _naviImage; }
            set
            {
                _naviImage = value;
                this.picNaviImage.Image = _naviImage;
            }
        }

        private string _naviName = "实时监控";
        [Browsable(true)]
        [Category("自定义属性")]
        [Description("导航按钮名称设置")]
        public string NaviName
        {
            get { return _naviName; }
            set
            {
                _naviName = value;
                this.lblNaviName.Text = _naviName;
            }
        }

        private bool _isActive = false;
        [Browsable(true)]
        [Category("自定义属性")]
        [Description("是否激活")]
        public bool IsActive
        {
            get { return _isActive; }
            set
            {
                _isActive = value;
                this.Invalidate();
            }
        }

        private int _activeGap = 0;
        [Browsable(true)]
        [Category("自定义属性")]
        [Description("激活方块边距")]
        public int ActiveGap
        {
            get { return _activeGap; }
            set
            {
                _activeGap = value;
                this.Invalidate();
            }
        }

        private int _activeHeight = 4;
        [Browsable(true)]
        [Category("自定义属性")]
        [Description("激活方块高度")]
        public int ActiveHeight
        {
            get { return _activeHeight; }
            set
            {
                _activeHeight = value;
                this.Invalidate();
            }
        }

        private Color _activeColor = Color.FromArgb(236, 240, 243);
        [Browsable(true)]
        [Category("自定义属性")]
        [Description("激活方块颜色")]
        public Color ActiveColor
        {
            get { return _activeColor; }
            set
            {
                _activeColor = value;
                this.Invalidate();
            }
        }

        [Browsable(true)]
        [Category("自定义属性")]
        [Description("悬浮渐变色系数")]
        public float ColorDepth { get; set; } = -0.2F;



        [Browsable(true)]
        [Category("自定义事件")]
        [Description("导航单击事件")]
        public event EventHandler ClickEvent;
        private void picNaviImage_Click(object sender, EventArgs e)
        {
            if (ClickEvent != null)
            {
                this.ClickEvent.Invoke(this, e);
            }
        }
        private void lblNaviName_Click(object sender, EventArgs e)
        {
            if (ClickEvent != null)
            {
                this.ClickEvent.Invoke(this, e);
            }
        }

        private void lblNaviName_MouseEnter(object sender, EventArgs e)
        {
            this.BackColor = ChangeColor(this.BackColor, ColorDepth);
        }

        private void lblNaviName_MouseLeave(object sender, EventArgs e)
        {
            this.BackColor = _saveBackColor;
        }


        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            Graphics graphics = e.Graphics;
            Rectangle rectangle = new Rectangle(ActiveGap, this.Height - ActiveHeight, this.Width - 2 * ActiveGap, ActiveHeight);
            graphics.FillRectangle(new SolidBrush(IsActive ? this.ActiveColor : (this.BackColor)), rectangle);

        }

        #region 颜色渐变
        private Color ChangeColor(Color color, float correctionFactor)
        {
            if (correctionFactor > 1.0f) correctionFactor = 1.0f;
            if (correctionFactor < -1.0f) correctionFactor = -1.0f;

            float red = (float)color.R;
            float green = (float)color.G;
            float blue = (float)color.B;

            if (correctionFactor < 0)
            {
                correctionFactor = correctionFactor + 1;
                red = red * correctionFactor;
                green = green * correctionFactor;
                blue = blue * correctionFactor;
            }
            else
            {
                red = (255 - red) * correctionFactor + red;
                green = (255 - green) * correctionFactor + green;
                blue = (255 - blue) * correctionFactor + blue;
            }

            if (red < 0) red = 0;
            if (red > 255) red = 255;
            if (green < 0) green = 0;
            if (green > 255) green = 255;
            if (blue < 0) blue = 0;
            if (blue > 255) blue = 255;

            return Color.FromArgb(color.A, (int)red, (int)green, (int)blue);
        }




        #endregion


    }
}
